using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace GameStateManager
{
    public class InputState
    {

        public KeyboardState currentKeyboardState;
        public List<GamePadState> currentGamePadState;

        public KeyboardState lastKeyboardState;
        public List<GamePadState> lastGamePadState;

        public InputState()
        {
            currentGamePadState = new List<GamePadState>();
            lastGamePadState = new List<GamePadState>();

            for (int i = 0; i < 5; i++)
            {
                currentGamePadState.Add(new GamePadState());
                lastGamePadState.Add(new GamePadState());
            }
        }

        public bool MenuItemUp
        {
            get { return (currentGamePadState[0].ThumbSticks.Left.Y > 0 && lastGamePadState[0].ThumbSticks.Left.Y <= 0); }
        }

        public bool MenuItemDown
        {
            get { return (currentGamePadState[0].ThumbSticks.Left.Y < 0 && lastGamePadState[0].ThumbSticks.Left.Y >= 0); }
        }

        public bool MenuItemSelect
        {
            get { return (currentGamePadState[0].Buttons.A == ButtonState.Pressed); }
        }

        public bool StartIsPressed(int player)
        {
            return (currentGamePadState[player].Buttons.Start == ButtonState.Pressed && lastGamePadState[player].Buttons.Start == ButtonState.Released);
        }

        PlayerIndex ConvertIntToPlayerIndex(int val)
        {
            PlayerIndex currentPlayer;

            switch (val)
            {
                case 0:
                    currentPlayer = PlayerIndex.One;
                    break;

                case 1:
                    currentPlayer = PlayerIndex.Two;
                    break;

                case 2:
                    currentPlayer = PlayerIndex.Three;
                    break;

                case 3:
                    currentPlayer = PlayerIndex.Four;
                    break;

                default:
                    currentPlayer = PlayerIndex.One;
                    break;
            }

            return currentPlayer;
        }

        public void Update()
        {
            lastKeyboardState = currentKeyboardState;

            for (int i = 0; i < 5; i++)
            {
                PlayerIndex currentPlayer = ConvertIntToPlayerIndex(i);
                
                if (GamePad.GetState(currentPlayer).IsConnected)
                {
                    lastGamePadState[i] = currentGamePadState[i];
                    currentGamePadState[i] = GamePad.GetState(currentPlayer);
                }
            }

            currentKeyboardState = Keyboard.GetState();
        }

        public bool IsNewKeyPress(Keys key)
        {
            return (currentKeyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key));
        }


        public GamePadState GetPlayerGamePad(int player)
        {
            return currentGamePadState[player];
        }

        public bool IsKeyDown(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key);
        }
    }
}
